// service/ShowtimeService.java
package main.java.com.cinema.service;

import main.java.com.cinema.model.Showtime;
import main.java.com.cinema.model.Hall;
import main.java.com.cinema.dao.ShowtimeDAO;
import main.java.com.cinema.util.DataStorage;
import main.java.com.cinema.util.Logger;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

public class ShowtimeService {
    private ShowtimeDAO showtimeDAO = new ShowtimeDAO();
    private TicketService ticketService = new TicketService();
    
    public List<Showtime> listAllShowtimes() {
        return showtimeDAO.findAll();
    }
    
    public Showtime findShowtimeById(String showtimeId) {
        return showtimeDAO.findById(showtimeId);
    }
    
    public List<Showtime> listShowtimesByDateRange(LocalDateTime start, LocalDateTime end) {
        return showtimeDAO.findByDateRange(start, end);
    }

    // service/ShowtimeService.java
    public boolean addShowtime(String operator, String movieId, String hallId,
                               LocalDateTime startTime, double price) {
        System.out.println("DEBUG: 开始添加场次");
        System.out.println("DEBUG: 参数 - movieId: " + movieId + ", hallId: " + hallId +
                ", startTime: " + startTime + ", price: " + price);

        // 参数验证
        if (movieId == null || hallId == null || startTime == null || price <= 0) {
            System.out.println("DEBUG: 参数验证失败");
            if (movieId == null) System.out.println("DEBUG: movieId为空");
            if (hallId == null) System.out.println("DEBUG: hallId为空");
            if (startTime == null) System.out.println("DEBUG: startTime为空");
            if (price <= 0) System.out.println("DEBUG: price小于等于0");
            return false;
        }

        // 检查放映厅是否存在
        Hall hall = DataStorage.getHallById(hallId);
        System.out.println("DEBUG: 查找放映厅 - hallId: " + hallId + ", hall: " + hall);
        if (hall == null) {
            System.out.println("DEBUG: 放映厅不存在！");
            return false;
        }

        // 检查时间是否在一周内
        LocalDateTime now = LocalDateTime.now();
        System.out.println("DEBUG: 当前时间: " + now);
        System.out.println("DEBUG: 开始时间: " + startTime);
        System.out.println("DEBUG: 一周后时间: " + now.plusDays(7));

        if (startTime.isBefore(now)) {
            System.out.println("DEBUG: 放映时间不能早于当前时间");
            return false;
        }

        if (startTime.isAfter(now.plusDays(7))) {
            System.out.println("DEBUG: 放映时间不能晚于一周后");
            return false;
        }

        // 创建新场次
        String showtimeId = "S" + UUID.randomUUID().toString().substring(0, 8).toUpperCase();
        System.out.println("DEBUG: 生成场次ID: " + showtimeId);

        Showtime showtime = new Showtime(showtimeId, movieId, hallId, startTime, price,
                hall.getRows(), hall.getSeatsPerRow());
        System.out.println("DEBUG: 创建Showtime对象成功");

        boolean result = showtimeDAO.save(showtime);
        System.out.println("DEBUG: 保存场次结果: " + result);

        if (result) {
            Logger.log("增加场次", "增加场次: " + startTime + " 在 " + hall.getName(), operator);
        }
        return result;
    }


    public boolean updateShowtime(String operator, String showtimeId, String newMovieId, 
                                 LocalDateTime newStartTime, double newPrice) {
        Showtime showtime = showtimeDAO.findById(showtimeId);
        if (showtime == null) {
            return false;
        }
        
        // 检查是否有票已售出
        if (ticketService.hasSoldTickets(showtimeId)) {
            return false; // 有票已售出，不允许修改
        }
        
        String oldMovieId = showtime.getMovieId();
        String oldStartTime = showtime.getStartTime().toString();
        
        // 更新场次信息
        if (newMovieId != null) {
            showtime.setMovieId(newMovieId);
        }
        if (newStartTime != null) {
            showtime.setStartTime(newStartTime);
        }
        if (newPrice > 0) {
            showtime.setPrice(newPrice);
        }
        
        boolean result = showtimeDAO.update(showtime);
        if (result) {
            Logger.log("修改场次", "修改场次信息: 时间从 " + oldStartTime + 
                      "，影片从 " + oldMovieId + " 修改为 " + showtime.getMovieId(), operator);
        }
        return result;
    }
    
    public boolean deleteShowtime(String operator, String showtimeId) {
        Showtime showtime = showtimeDAO.findById(showtimeId);
        if (showtime == null) {
            return false;
        }
        
        // 检查是否有票已售出
        if (ticketService.hasSoldTickets(showtimeId)) {
            return false; // 有票已售出，不允许删除
        }
        
        boolean result = showtimeDAO.delete(showtimeId);
        if (result) {
            Logger.log("删除场次", "删除场次信息: 时间 " + showtime.getStartTime() + 
                      "，影片 " + showtime.getMovieId(), operator);
        }
        return result;
    }
    
    // 自动排片功能
    public void autoSchedule(String operator) {
        // 这里可以实现自动排片逻辑
        // 根据影片评分安排黄金时段等
        // 为简化，这里只是示意
        Logger.log("自动排片", "执行自动排片功能", operator);
    }
}
